﻿using Bogus;
using FluentMigrator;

namespace WebForm.Migrator
{
    [Migration(1)]
    public class UserMigration : Migration
    {
        public override void Up()
        {
            Create.Table("Users")
                .WithColumn("Id").AsInt32().PrimaryKey().Identity()
                .WithColumn("Name").AsString(255).NotNullable()
                .WithColumn("Email").AsString(255).NotNullable()
                .WithColumn("Password").AsString(255).NotNullable()
                .WithColumn("Gender").AsInt32() // 修改性别字段为整数类型
                .WithColumn("PhoneNumber").AsString(30)
                .WithColumn("DateOfBirth").AsDateTime(); // 出生日期

            InsertUsers();
        }

        private void InsertUsers()
        {
            var userFaker = new Faker<Users>()
                .RuleFor(u => u.Name, f => f.Name.FullName())
                .RuleFor(u => u.Email, f => f.Internet.Email())
                .RuleFor(u => u.Password, f => "123456") // 注意：实际应用中不要使用明文密码
                .RuleFor(u => u.Gender, f => f.Random.Int(0, 1)) // 修改随机生成性别的方式
                .RuleFor(u => u.PhoneNumber, f => f.Phone.PhoneNumber()) // 生成随机的电话号码
                .RuleFor(u => u.DateOfBirth, f => f.Date.Past(80));

            var users = userFaker.Generate(20);

            foreach (var user in users)
            {
                Insert.IntoTable("Users")
                    .Row(new 
                    {
                        user.Name,
                        user.Email, 
                        user.Password,
                        user.Gender,
                        user.PhoneNumber,
                        user.DateOfBirth,
                    });
            }
        }

        public override void Down()
        {
            Delete.Table("Users");
        }
    }
}
